﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WB.Mvc.Forms.Widgets;

namespace WB.Mvc.Forms
{
	public class IntegerField: Field
	{
		private bool isValid = true;

		/// <summary>
		/// Creates an instance of <see cref="IntegerField"/>.
		/// </summary>
		public IntegerField(string name) : base(name)
		{
		}

		/// <summary>
		/// Gets the widget used to render the input for this field.
		/// </summary>
		public override Widget GetWidget()
		{
			return new TextInput(Name);
		}

		/// <summary>
		/// Validate this field.
		/// </summary>
		public override void Validate()
		{
			isValid = false;

			if (Value == null)
				return;
			
			if (Value is int)
			{
				isValid = true;
			}
			else
			{
				int throwMeAway;
				isValid = int.TryParse(Convert.ToString(Value), out throwMeAway);
			}
		}

		/// <summary>
		/// Whether this field's value is valid.
		/// </summary>
		public override bool IsValid
		{
			get
			{
				return isValid;
			}
		}
	}
}
